// 计算两个数的最大公约数
function getGCD(a, b) {
  while (b !== 0) {
    const temp = a % b;
    a = b;
    b = temp;
  }
  return a;
}

function minSumAfterOps(arr) {
  const groups = {};

  for (let i = 1; i <= arr.length; i++) {
    const groupId = Math.floor(arr.length / i);
    if (!groups[groupId]) {
      groups[groupId] = [];
    }
    groups[groupId].push(i - 1);
  }

  let sum = 0;

  for (const key in groups) {
    const list = groups[key];

    if (list.length === 1) {
      sum += arr[list[0]];
      continue;
    }

    let s = arr[list[0]];
    for (let i = 1; i < list.length; i++) {
      s = getGCD(s, arr[list[i]]);
    }

    sum += s * list.length;
  }
  return sum;
}
console.log(minSumAfterOps([1, 2, 3, 7, 8, 9]));
